import os
import pandas as pd
from flask import request, send_from_directory
from dash import get_app
from dash import (
    html,
    callback,
    Output,
    Input,
    State,
    register_page,
)

from ddbtools import (
    BaseCRUD,
    DBDf,
    create_db,
    create_table,
    DbColumn,
    get_all_dbs,
    get_all_tables,
    get_table_info,
    get_table_columns,
)
from gypb.db import get_db
import feffery_antd_components as fac

app = get_app()
register_page(__name__, name="数据库表字段管理", order=3)

with get_db() as session:
    dbinfodf = get_all_dbs(session)
    tableinfodf = pd.concat([
        get_table_info(session, db, table)
        for db in dbinfodf.dbpath
        for table in get_all_tables(session, db)
    ],axis=1).T.sort_values(by=["db_name", "table_name"])

layout = html.Div(
    [
        fac.AntdTable(
            id="table-db-overview",
            columns=[
                {"title": c, "dataIndex": c, "editable": False}
                for c in ["数据库", "分区方案", "分区类型", "引擎"]
            ],
            data=[
                {
                    "数据库": row["dbpath"],
                    "分区方案": row["partitionPlan"],
                    "分区类型": row["partitiontype"],
                    "引擎": row["engineType"],
                }
                for row in dbinfodf.to_dict("records")
            ],
            maxHeight=500,
        ),
        fac.AntdTable(
            id="table-dbtable-overview",
            columns=[
                {"title": c, "dataIndex": c, "editable": False ,}
                for c in [
                    "数据库",
                    "表名",
                    "分区列",
                    "排序列",
                    "去重方案",
                    "排序列降维函数",
                ]
            ],
            data=[
                {
                    "数据库":row["db_name"],
                    "表名":row["table_name"],
                    "分区列":row["partition_columns"],
                    "排序列":row["sort_columns"],
                    "去重方案":row["keep_duplicates"],
                    "排序列降维函数":row["sort_key_mapping_function"],
                    # "操作":
                    "key": f"{row['db_name']}-{row['table_name']}",
                }
                for row in tableinfodf.to_dict("records")
            ],
            maxHeight=500,
            bordered=True,
            expandedRowKeyToContent=[
                {
                    "key": f"{row['db_name']}-{row['table_name']}",
                    "content": fac.AntdTable(
                        columns=[
                            {"title": c, "dataIndex": c, "editable": False}
                            for c in [
                                "列名",
                                "数据类型",
                                "压缩方案",
                                "备注",
                            ]
                        ],
                        data=[
                            {
                                "列名": col["name"],
                                "数据类型": col["typeString"],
                                "压缩方案": col["compress_methods"],
                                "备注": col["comment"],
                            }
                            for col in row["col_defs"].reset_index().to_dict("records")
                        ]
                    ),
                }
                for row in tableinfodf.to_dict("records")
            ],
        ),
    ]
)
